/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import java.io.PrintStream;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import junit.framework.AssertionFailedError;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestProvisioningUtil
/*     */   extends TestCase
/*     */ {
/*  43 */   private static String NAME_ROOT_DOMAIN = "ldaptest";
/*     */   
/*     */   public static String genTestId() {
/*  46 */     Date date = new Date();
/*  47 */     SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd-HHmmss");
/*  48 */     return fmt.format(date);
/*     */   }
/*     */   
/*     */   public static String baseDomainName(String testName) {
/*  52 */     return testName + "-" + genTestId() + "." + NAME_ROOT_DOMAIN;
/*     */   }
/*     */   
/*     */   public static String baseDomainName(String testName, String testId) {
/*  56 */     if (testId == null) {
/*  57 */       return testName + "." + NAME_ROOT_DOMAIN;
/*     */     }
/*  59 */     return testName + "-" + testId + "." + NAME_ROOT_DOMAIN;
/*     */   }
/*     */   
/*     */   public static void verifySameId(NamedEntry entry1, NamedEntry entry2) throws Exception {
/*  63 */     assertNotNull(entry1);
/*  64 */     assertNotNull(entry2);
/*  65 */     assertEquals(entry1.getId(), entry2.getId());
/*     */   }
/*     */   
/*     */   public static void verifySameEntry(NamedEntry entry1, NamedEntry entry2) throws Exception
/*     */   {
/*  70 */     verifySameId(entry1, entry2);
/*  71 */     assertEquals(entry1.getName(), entry2.getName());
/*     */   }
/*     */   
/*     */   public static void verifyEntries(List<NamedEntry> list, NamedEntry[] entries, boolean checkCount) throws Exception
/*     */   {
/*     */     try
/*     */     {
/*  78 */       if (checkCount) {
/*  79 */         assertEquals(list.size(), entries.length);
/*     */       }
/*  81 */       Set<String> ids = new HashSet();
/*  82 */       for (NamedEntry entry : list) {
/*  83 */         ids.add(entry.getId());
/*     */       }
/*  85 */       for (NamedEntry entry : entries) {
/*  86 */         assertTrue(ids.contains(entry.getId()));
/*  87 */         ids.remove(entry.getId());
/*     */       }
/*     */       
/*     */ 
/*  91 */       if (checkCount) {
/*  92 */         assertEquals(ids.size(), 0);
/*     */       }
/*     */     } catch (AssertionFailedError e) {
/*  95 */       System.out.println("\n===== verifyEntries failed =====");
/*  96 */       System.out.println("Message:" + e.getMessage());
/*     */       
/*  98 */       System.out.println("\nlist contains " + list.size() + " entries:");
/*  99 */       for (NamedEntry entry : list)
/* 100 */         System.out.println("    " + entry.getName());
/* 101 */       System.out.println("entries contains " + entries.length + " entries:");
/* 102 */       for (NamedEntry entry : entries) {
/* 103 */         System.out.println("    " + entry.getName());
/*     */       }
/* 105 */       System.out.println();
/* 106 */       throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   public static void verifyEntriesById(List<NamedEntry> list, String[] names, boolean checkCount)
/*     */     throws Exception
/*     */   {
/* 113 */     Set<String> idsInList = new HashSet();
/* 114 */     for (NamedEntry entry : list) {
/* 115 */       idsInList.add(entry.getId());
/*     */     }
/* 117 */     verifyEntries(idsInList, names, checkCount);
/*     */   }
/*     */   
/*     */   public static void verifyEntriesByName(List<NamedEntry> list, String[] names, boolean checkCount)
/*     */     throws Exception
/*     */   {
/* 123 */     Set<String> namesInList = new HashSet();
/* 124 */     for (NamedEntry entry : list) {
/* 125 */       namesInList.add(entry.getName());
/*     */     }
/* 127 */     verifyEntries(namesInList, names, checkCount);
/*     */   }
/*     */   
/*     */   public static void verifyEntries(Set<String> list, String[] names, boolean checkCount) throws Exception
/*     */   {
/*     */     try
/*     */     {
/* 134 */       if (checkCount) {
/* 135 */         assertEquals(names.length, list.size());
/*     */       }
/* 137 */       for (String name : names) {
/* 138 */         assertTrue(list.contains(name));
/*     */       }
/*     */     } catch (AssertionFailedError e) {
/* 141 */       System.out.println("\n===== verifyEntries failed =====");
/* 142 */       System.out.println("Message:" + e.getMessage());
/*     */       
/* 144 */       System.out.println("\nlist contains " + list.size() + " entries:");
/* 145 */       for (String name : list)
/* 146 */         System.out.println("    " + name);
/* 147 */       System.out.println("entries contains " + names.length + " entries:");
/* 148 */       for (String name : names) {
/* 149 */         System.out.println("    " + name);
/*     */       }
/* 151 */       System.out.println();
/* 152 */       throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   public static void verifyEquals(Set<String> expected, Set<String> actual) throws Exception
/*     */   {
/* 158 */     assertEquals(expected.size(), actual.size());
/*     */     
/* 160 */     for (String e : expected) {
/* 161 */       assertTrue(actual.contains(e));
/*     */     }
/* 163 */     for (String a : actual) {
/* 164 */       assertTrue(expected.contains(a));
/*     */     }
/*     */   }
/*     */   
/*     */   public static LdapProv getLdapProvisioning() throws ServiceException {
/* 169 */     LdapProv lp = (LdapProv)Provisioning.getInstance();
/* 170 */     return lp;
/*     */   }
/*     */   
/*     */   public static SoapProvisioning getSoapProvisioning() throws ServiceException {
/* 174 */     SoapProvisioning sp = new SoapProvisioning();
/* 175 */     sp.soapSetTransportTimeout(0);
/* 176 */     sp.soapSetURI("https://localhost:7071/service/admin/soap/");
/* 177 */     sp.soapZimbraAdminAuthenticate();
/* 178 */     return sp;
/*     */   }
/*     */   
/*     */   public static SoapProvisioning getSoapProvisioning(String userName, String password) throws ServiceException {
/* 182 */     SoapProvisioning sp = new SoapProvisioning();
/* 183 */     sp.soapSetURI("https://localhost:7071/service/admin/soap/");
/* 184 */     sp.soapAdminAuthenticate(userName, password);
/* 185 */     return sp;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static SoapProvisioningUser getSoapProvisioningUser(String userName, String password)
/*     */     throws ServiceException
/*     */   {
/* 196 */     SoapProvisioningUser spu = new SoapProvisioningUser(userName, password);
/* 197 */     spu.auth();
/* 198 */     return spu;
/*     */   }
/*     */   
/*     */   public static class SoapProvisioningUser extends SoapProvisioning
/*     */   {
/*     */     String mName;
/*     */     String mPassword;
/*     */     
/*     */     SoapProvisioningUser(String name, String password) {
/* 207 */       this.mName = name;
/* 208 */       this.mPassword = password;
/* 209 */       setURL();
/*     */     }
/*     */     
/*     */     private void setURL() {
/* 213 */       soapSetURI(TestUtil.getSoapUrl());
/*     */     }
/*     */     
/*     */     private void auth() throws ServiceException {
/* 217 */       Element.XMLElement req = new Element.XMLElement(AccountConstants.AUTH_REQUEST);
/* 218 */       Element a = req.addElement("account");
/* 219 */       a.addAttribute("by", "name");
/* 220 */       a.setText(this.mName);
/* 221 */       req.addElement("password").setText(this.mPassword);
/* 222 */       Element response = invoke(req);
/* 223 */       String authToken = response.getElement("authToken").getText();
/* 224 */       setAuthToken(new ZAuthToken(authToken));
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */     public Element invokeOnTargetAccount(Element request, String targetId)
/*     */       throws ServiceException
/*     */     {
/* 232 */       return super.invokeOnTargetAccount(request, targetId);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestProvisioningUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */